﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class GameManager : MonoBehaviour
{
    public List<Enemy> pigs;
    public List<Bird> birds;
    private Vector3 initial_position;
    public static GameManager _instance;
    public GameObject[] stars;
    public GameObject win;
    public GameObject lose;
    public GameStart game_start;
    private string now_level;
    private string[] temp_level = new string[2];

    //单例模式下实例化管理者

    private void Awake()
    {
        _instance = this;
        initial_position = birds[0].transform.position;
    }

    private void Start()
    {
        Initialized();
    }

    public void Initialized()  //新一轮小鸟上线
    {
        for (int i = 0; i<birds.Count; i++)
        {
            if (i == 0)
            {
                birds[i].transform.position = initial_position;
                birds[i].enabled = true;
                birds[i].sj.enabled = true;
            }
            else
            {
                birds[i].enabled = false;
                birds[i].sj.enabled = false;
            }
        }
    }
    //判断输赢情况或者下一只鸟
    public bool NextBird()
    {
        if (pigs.Count > 0)
        {
            if(birds.Count > 0)
            {
                Initialized();
                return true;
            }
            else
            {
                lose.SetActive(true);
                return false;
            }
        }
        else
        {
            WriteData();
            win.SetActive(true);
            return false;
        }
    }

    public void Show()
    {
        StartCoroutine("ShowStars");
    }

    IEnumerator ShowStars()  //是IEnumerator不是IEnumerable
    {
        for (int i = 0; i <= birds.Count; i++)
        {
            if (i >= stars.Length)
            {
                break;
            }
            yield return new WaitForSeconds(0.2f); //延时0.2秒，有序显示
            stars[i].SetActive(true);
        }
    }
    
    //游戏结束后需要记录数据
    public void Replay() //重玩
    {
        SceneManager.LoadScene(2);
    }

    public void GoHome() //回家
    {
        SceneManager.LoadScene(1);
    }

    public void NextLevel()
    {
        temp_level = PlayerPrefs.GetString("now_number").Split('-'); //字符串分割
        if (int.Parse(temp_level[1]) < 3) //后面暂无关卡
        {
            //切换关卡名字
            temp_level[1] = (int.Parse(temp_level[1]) + 1).ToString();
            now_level = temp_level[0] + "-" + temp_level[1];
            print(now_level);
            PlayerPrefs.SetString("now_number",now_level);
            //切换关卡显示
            SceneManager.LoadScene(2);
        }
    }

    private void WriteData()
    {
        //更新关卡星星数
        now_level = game_start.now_level;
        if (PlayerPrefs.GetInt(now_level, 0) != 3 && PlayerPrefs.GetInt(now_level, 0) < birds.Count + 1)
        {
            if (birds.Count >= 2)
            {
                PlayerPrefs.SetInt(now_level, 3);
            }
            else
            {
                PlayerPrefs.SetInt(now_level, birds.Count + 1);
            }
        }
    }

    //暂停时离开的不记录数据
    public void PauseReplay()  //暂停后重玩
    {
        Time.timeScale = 1;
        SceneManager.LoadScene(2);
    }

    public void PauseGoHome()  //暂停后回家
    {
        Time.timeScale = 1;
        SceneManager.LoadScene(1);
    }
}
